package contest1609;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class JQueueII {
    static Scanner cin = new Scanner(new BufferedInputStream(System.in));//大量输入优化版本
    static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));//大量输出优化版本
    static PrintWriter cerr = new PrintWriter(System.err, true);//开启调试模式,一般输出量不大，不需要buffered

    public static void main(String[] args) {
        //TODO AOJ开启你的ACM传奇人生
        cerr.println("----Start your ACM legendary life with AOJ(www.webturing.com)----");
        Queue[] Q = new Queue[cin.nextInt()];
        for (int i = 0; i < Q.length; i++) {
            Q[i] = new Queue();
        }
        int q = cin.nextInt();
        while (q-- > 0) {
            int type = cin.nextInt();
            if (type == 0) {
                int t = cin.nextInt();
                int x = cin.nextInt();
                Q[t].push(x);
            } else if (type == 1) {
                int t = cin.nextInt();
                if (Q[t].empty()) continue;
                cout.println(Q[t].front());
            } else {
                int t = cin.nextInt();
                if (Q[t].empty()) continue;
                Q[t].pop();
            }
        }

        cin.close();//关闭输入流
        cout.close();//关闭输出流，这是必须的，否则一般不会自动刷新缓冲区

    }

    static class Queue {
        int[] data = new int[200000];
        int head = 0;
        int tail = -1;

        void push(int x) {
            data[++tail] = x;
        }

        void pop() {
            ++head;
        }

        int front() {
            return data[head];
        }

        boolean empty() {
            return head > tail;
        }
    }
}